﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ChuanYe.CountyFireManagement.Api.Models;
using ChuanYe.CountyFireManagement.BLL;
using ChuanYe.CountyFireManagement.EF.MySQL.Models;
using ChuanYe.CountyFireManagement.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace ChuanYe.CountyFireManagement.Api.Controllers
{
    /// <summary>
    /// 定位设备记录
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class GpsDeviceRecordController : ControllerBase
    {
        /// <summary>
        /// 批量增加 定位设备记录 信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultDtoBase Add(GpsDeviceRecordArrayRequest obj)
        {
            ResultDtoBase result = new ResultDtoBase();
            try
            {
                GpsDeviceBLL.Instance.Add(new Gpsdevice()
                {
                    CreateTime = DateTime.Now,
                    GpsDeviceNo = obj.GpsDeviceRecords.FirstOrDefault().GpsDeviceNo,
                    GpsDeviceName = DateTime.Now.ToString("yyyyMMddHHmmsss"),
                });
                var gpsdeviceInfo = GpsDeviceBLL.Instance.Get(obj.GpsDeviceRecords.FirstOrDefault().GpsDeviceNo);

                var infos = new List<Gpsdevicerecord>();
                obj.GpsDeviceRecords=obj.GpsDeviceRecords.OrderBy(o => o.RecordTime).ToList();
                foreach (var item in obj.GpsDeviceRecords)
                {
                    Gpsdevicerecord info = new Gpsdevicerecord();
                    info.GpsDeviceRecordGuid = item.GpsDeviceRecordGuid;
                    info.GpsDeviceId = gpsdeviceInfo.GpsDeviceId;
                    info.Latitude = item.Latitude;
                    info.Longitude = item.Longitude;
                    info.RecordTime = item.RecordTime;
                    info.Speed = item.Speed;
                    info.Accuracy = item.Accuracy;
                    info.Altitude = item.Altitude;
                    info.Bearing = item.Bearing;
                    info.CreateTime = item.CreateTime ?? DateTime.Now;
                    infos.Add(info);
                }
                result.Status = GpsDeviceRecordBLL.Instance.AddList(infos);
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/GpsDeviceRecord/Add Server Error";
                LogHelper.Error(result.Message, ex);
            }
            return result;
        }


        /// <summary>
        ///  获取 定位设备记录 分页
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultDto<dynamic, dynamic> GetByPage(GpsDeviceRecordShearchRequest obj)
        {
            ResultDto<dynamic, dynamic> result = new ResultDto<dynamic, dynamic>();
            try
            {
                var page = Pickout.SetPage(obj);
                result.FirstParam = GpsDeviceRecordBLL.Instance.GetByPage(obj.GpsDeviceNo, page);
                result.SecondParam = page.TotalRows;
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/GpsDeviceRecord/GetByPage Server Error";
                LogHelper.Error(result.Message, ex);
            }
            return result;
        }

        /// <summary>
        ///  获取 设备 分页
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultDto<dynamic> GetGpsDevice()
        {
            ResultDto<dynamic> result = new ResultDto<dynamic>();
            try
            {
                result.FirstParam = GpsDeviceRecordBLL.Instance.GetGpsDevice();
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/GpsDeviceRecord/GetGpsDevice Server Error";
                LogHelper.Error(result.Message, ex);
            }
            return result;
        }
    }
}